﻿using System.Linq;
using System.Web.Mvc;
using System.Threading;
using System;

namespace Library.Web.Controllers
{
    public class BooksController : BaseController
    {

        public ActionResult Index(int? page)
        {
            // called at first load of book/index
            var model = new ViewModels.BookViewModel();
            model.GetBookPage(page ?? 1);

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(int? page, ViewModels.BookViewModel viewModel)
        {
            // called on changing pages
            // includes all the data from filtering
            viewModel.GetBookPage(page ?? 1);
            return View(viewModel);
        }

        public ActionResult Details(int id)
        {
            var model = new ViewModels.BookDetailsViewModel();
            model.GetBookComments(id, 1);

            if (model.Book != null)
                return View(model);

            return RedirectToAction("Index");
        }

        public ActionResult GetBookComments(int bookId, int pageNumber)
        {
            var model = new ViewModels.BookDetailsViewModel();
            model.GetBookComments(bookId, pageNumber);

            Thread.Sleep(TimeSpan.FromSeconds(2));

            return PartialView("BookComments", model.BookComments.Result);
        }
    }
}
